package com.sinodata.bsm.cicp.collector.net.radware;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.union.net.discovery.agent.ext.Radware;
import com.union.net.discovery.agent.ext.Radware.RsWSDApplicationServerEntry;

public class RWServerCollector extends Collector {
    private static Logger logger = Logger.getLogger(RWServerCollector.class);

    static String[] ParamMeta = { "共同体", "服务名称" };
    static String[] PropMeta = { "管理状态", "操作状态", "连接数限制", "带宽限制", "当前连接数" };

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        try {
            Radware rw = Radware.getRadware(ip, getParamMap().get(ParamMeta[0]));
            if (rw != null && rw.getRsWSDApplicationServerTable() != null) {
                List<RsWSDApplicationServerEntry> servers = rw.getRsWSDApplicationServerTable();
                String serverName = getParamMap().get(ParamMeta[1]);
                for (RsWSDApplicationServerEntry entry : servers) {
                    if (serverName.equals(entry.getRsWSDServerName())) {

                        String adminstatus = entry.getRsWSDServerAdminStatus();
                        if ("1".equals(adminstatus)) {
                            adminstatus = "enable";
                        } else if ("2".equals(adminstatus)) {
                            adminstatus = "disable";
                        } else if ("3".equals(adminstatus)) {
                            adminstatus = "shutdown";
                        }

                        String operstatus = entry.getRsWSDServerOperStatus();
                        if ("1".equals(operstatus)) {
                            operstatus = "active";
                        } else if ("2".equals(operstatus)) {
                            operstatus = "notInService";
                        } else if ("3".equals(operstatus)) {
                            operstatus = "noNewSessions";
                        }

                        ResPropertyValue propertyValueX = new ResPropertyValue();
                        propertyValueX.setValues(ResPropertyValue.createPropValue(adminstatus, PropMeta[0]), ResPropertyValue.createPropValue(operstatus, PropMeta[1]), ResPropertyValue.createPropValue(entry.getRsWSDServerConnectionLimit(), PropMeta[2]), ResPropertyValue.createPropValue(entry.getRsWSDServerBandWidth(), PropMeta[3]), ResPropertyValue.createPropValue(entry.getRsWSDServerAttachedUsersNumber(), PropMeta[4]));
                        return Arrays.asList(propertyValueX);
                    }

                }
            }
        } catch (Exception e) {
            logger.error("Radware server collect error!", e);
            throw new CollectorException("Radware server collect error!", e);
        }

        return null;
    }

    public String[] getParamMeta() {
        return ParamMeta;
    }

    public String[] getPropMeta() {
        return PropMeta;
    }

}
